package insta.tp.view;

import java.util.InputMismatchException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class View {
	
	static java.util.Scanner sc = new java.util.Scanner(System.in);
	
	public static int saisie(String lib, String title, int inf, int sup){
		try {
			int choix;
			String s = new String();
			JFrame frame = new JFrame();
			do {
				s = JOptionPane.showInputDialog(frame, lib, title, JOptionPane.QUESTION_MESSAGE);
				if(s==null) return 0;
				choix = Integer.parseInt(s);
			} while(choix<inf||choix>sup);
			return choix;
		} catch(NumberFormatException e) {
			affiche("Erreur de saisie, recommencez! ");
			return saisie(lib, title, inf, sup);
		}
	}
	
	public static String saisie(String[] item, String question, String title) {
		try {
			JFrame frame = new JFrame();
		    return (String) JOptionPane.showInputDialog(frame, question, title, JOptionPane.QUESTION_MESSAGE, null, item, item[0]);
		}
		catch(Exception e) {
			affiche("Erreur de saisie, recommencez! ");
			return saisie(item, question, title);
		}
	}
	
	public static String saisie(String lib, String title) {
		try {
			JFrame frame = new JFrame();
			String choix = JOptionPane.showInputDialog(frame, lib, title, JOptionPane.QUESTION_MESSAGE);
			return choix;
		} catch(InputMismatchException e) {
			affiche("Erreur de saisie, recommencez: ");
			sc.nextLine();
			return saisie(lib, title);
		}
	}

	public static void affiche(String mes){
		JOptionPane.showMessageDialog(null,mes);
	}

}
